home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / stdlib / strtod.c < prev    next >
C/C++ Source or Header  |  1992-03-06  |  4KB  |  180 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <localeinfo.h>
  21. #include <errno.h>
  22. #include <float.h>
  23. #include <ctype.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <math.h>
  27.  
  28.  
  29. /* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the
  30.    character after the last one used in the number is put in *ENDPTR.  */
  31. double
  32. DEFUN(strtod, (nptr, endptr), CONST char *nptr AND char **endptr)
  33. {
  34.   register CONST char *s;
  35.   short int sign;
  36.   wchar_t decimal;    /* Decimal point character.  */
  37.  
  38.   /* The number so far.  */
  39.   double num;
  40.  
  41.   int got_dot;        /* Found a decimal point.  */
  42.   int got_digit;    /* Seen any digits.  */
  43.  
  44.   /* The exponent of the number.  */
  45.   long int exponent;
  46.  
  47.   if (nptr == NULL)
  48.     {
  49.       errno = EINVAL;
  50.       goto noconv;
  51.     }
  52.  
  53.   /* Figure out the decimal point character.  */
  54.   if (mbtowc(&decimal, _numeric_info->decimal_point, 1) <= 0)
  55.     decimal = (wchar_t) *_numeric_info->decimal_point;
  56.  
  57.   s = nptr;
  58.  
  59.   /* Eat whitespace.  */
  60.   while (isspace(*s))
  61.     ++s;
  62.  
  63.   /* Get the sign.  */
  64.   sign = *s == '-' ? -1 : 1;
  65.   if (*s == '-' || *s == '+')
  66.     ++s;
  67.  
  68.   num = 0.0;
  69.   got_dot = 0;
  70.   got_digit = 0;
  71.   exponent = 0;
  72.   for (;; ++s)
  73.     {
  74.       if (isdigit (*s))
  75.     {
  76.       got_digit = 1;
  77.  
  78.       /* Make sure that multiplication by 10 will not overflow.  */
  79.       if (num > DBL_MAX * 0.1)
  80.         /* The value of the digit doesn't matter, since we have already
  81.            gotten as many digits as can be represented in a `double'.
  82.            This doesn't necessarily mean the result will overflow.
  83.            The exponent may reduce it to within range.
  84.  
  85.            We just need to record that there was another
  86.            digit so that we can multiply by 10 later.  */
  87.         ++exponent;
  88.       else
  89.         num = (num * 10.0) + (*s - '0');
  90.  
  91.       /* Keep track of the number of digits after the decimal point.
  92.          If we just divided by 10 here, we would lose precision.  */
  93.       if (got_dot)
  94.         --exponent;
  95.     }
  96.       else if (!got_dot && (wchar_t) *s == decimal)
  97.     /* Record that we have found the decimal point.  */
  98.     got_dot = 1;
  99.       else
  100.     /* Any other character terminates the number.  */
  101.     break;
  102.     }
  103.  
  104.   if (!got_digit)
  105.     goto noconv;
  106.  
  107.   if (tolower(*s) == 'e')
  108.     {
  109.       /* Get the exponent specified after the `e' or `E'.  */
  110.       int save = errno;
  111.       char *end;
  112.       long int exp;
  113.  
  114.       errno = 0;
  115.       ++s;
  116.       exp = strtol(s, &end, 10);
  117.       if (errno == ERANGE)
  118.     {
  119.       /* The exponent overflowed a `long int'.  It is probably a safe
  120.          assumption that an exponent that cannot be represented by
  121.          a `long int' exceeds the limits of a `double'.  */
  122.       if (endptr != NULL)
  123.         *endptr = end;
  124.       if (exp < 0)
  125.         goto underflow;
  126.       else
  127.         goto overflow;
  128.     }
  129.       else if (end == s)
  130.     /* There was no exponent.  Reset END to point to
  131.        the 'e' or 'E', so *ENDPTR will be set there.  */
  132.     end = (char *) s - 1;
  133.       errno = save;
  134.       s = end;
  135.       exponent += exp;
  136.     }
  137.  
  138.   if (endptr != NULL)
  139.     *endptr = (char *) s;
  140.  
  141.   if (num == 0.0)
  142.     return 0.0;
  143.  
  144.   /* Multiply NUM by 10 to the EXPONENT power,
  145.      checking for overflow and underflow.  */
  146.  
  147.   if (exponent < 0)
  148.     {
  149.       if (num < DBL_MIN * pow(10.0, (double) -exponent))
  150.     goto underflow;
  151.     }
  152.   else if (exponent > 0)
  153.     {
  154.       if (num > DBL_MAX * pow(10.0, (double) -exponent))
  155.     goto overflow;
  156.     }
  157.  
  158.   num *= pow(10.0, (double) exponent);
  159.  
  160.   return num * sign;
  161.  
  162.  overflow:
  163.   /* Return an overflow error.  */
  164.   errno = ERANGE;
  165.   return HUGE_VAL * sign;
  166.  
  167.  underflow:
  168.   /* Return an underflow error.  */
  169.   if (endptr != NULL)
  170.     *endptr = (char *) nptr;
  171.   errno = ERANGE;
  172.   return 0.0;
  173.  
  174.  noconv:
  175.   /* There was no number.  */
  176.   if (endptr != NULL)
  177.     *endptr = (char *) nptr;
  178.   return 0.0;
  179. }
  180.